<template>
  <div class="content">
    <div
      class="body-wrapper"
      :style="{
        backgroundImage: props.backgroundImg?.length ? `url('${props.backgroundImg[0].url}')` : ''
      }"
    >
      <img
        src="https://qafanle.oss-cn-hangzhou.aliyuncs.com/img/2025/01/02/抽奖_20250102145006A279.png"
        style="width: 375px; height: 866px"
      />
      <div class="list">
        <div class="item" v-for="item in getList" :key="item.id">
          <div
            class="div-wrapper"
            :style="`transform:rotate(${item.rotage}deg);z-index: ${item.zIndex};background:${item.background};`"
          >
            <div class="cover" :style="`rotate: ${getList.length > 1 ? item.deg / 2 : 0}deg;`">
              <img v-show="item.img" :src="item.img" />
              <div class="name">{{ item.name1 }}</div>
              <div class="name name2">{{ item.name2 }}</div>
            </div>
          </div>
        </div>
        <img
          class="arrow-cover"
          src="https://qafanle.oss-cn-hangzhou.aliyuncs.com/img/2024/12/30/Group 53504_20241230101701A243.png"
          alt=""
        />
      </div>
      <div class="award-list-wapprer">
        <div
          class="award-list-item"
          v-for="item in getList"
          :key="item.id"
          v-show="item.name !== '谢谢参与'"
        >
          <div class="award-list-item-img">
            <img :src="item.img" v-show="item.img" />
          </div>
          <div class="award-list-item-name">{{ item.name }}</div>
        </div>
      </div>
      <div class="left" @click="scrollLeft"></div>
      <div class="right" @click="scrollRight"></div>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  list: {
    type: Array,
    default() {
      return []
    }
  },
  backgroundImg: {
    type: Array,
    default() {
      return []
    }
  }
})

function foamatList(list) {
  return list.map((item, index) => {
    let n = 360 / list.length
    let color = index % 2 === 0 ? '#91E850' : '#CCFF9A'
    return {
      ...item,
      img: item.rewardImageUrl?.length ? item.rewardImageUrl[0].url : '',
      deg: n,
      rotage: index * n,
      background: `conic-gradient(${color} 0deg ${n}deg, transparent ${n}deg 360deg)`,
      zIndex: index + 10,
      name1: item.name?.length > 5 ? item.name?.slice(0, 5) : item.name,
      name2: item.name?.length > 5 ? item.name?.slice(5, item.name?.length) : ''
    }
  })
}

const getList = computed(() => {
  const arr = props.list.map((i) => {
    return {
      ...i,
      rewardProbability: i.rewardProbability ? i.rewardProbability : 0
    }
  })
  let sum = arr.length ? arr.reduce((acc, cur) => acc + cur.rewardProbability, 0) : 0
  console.log('sum', sum)
  let list = []
  if (sum < 100) {
    list = [
      {
        id: 0,
        name: '谢谢参与',
        rewardImageUrl: [
          {
            url: 'https://qafanle.oss-cn-hangzhou.aliyuncs.com/img/2024/12/30/谢谢参与_20241230094940A242.png'
          }
        ]
      }
    ]
  } else {
    list = []
  }

  props.list.forEach((i) => {
    list.push({ ...i })
  })
  return foamatList(list)
})

// 左
function scrollLeft() {
  const dataBar = document.querySelector('.award-list-wapprer')
  dataBar.scrollTo({
    left: dataBar.scrollLeft - dataBar.clientWidth,
    behavior: 'smooth'
  })
}

// 右
function scrollRight() {
  const dataBar = document.querySelector('.award-list-wapprer')
  dataBar.scrollTo({
    left: dataBar.scrollLeft + dataBar.clientWidth,
    behavior: 'smooth'
  })
}
</script>

<style lang="scss" scoped>
.content {
  width: 375px;
  height: 866px;
  position: relative;
  .body-wrapper {
    background-repeat: no-repeat;
    background-size: 100%;
    .canvas-wrapper {
      position: absolute;
      top: 211.47px;
      left: 51.47px;
      z-index: 999;
    }
    .list {
      position: absolute;
      top: 188px;
      left: 28px;
      width: 320px;
      height: 320px;
      .item {
        .div-wrapper {
          position: absolute;
          left: 23px;
          top: 23px;
          width: 275px;
          height: 275px;
          border-radius: 50%; /* 使元素呈圆形 */
          z-index: 1;
        }
        .cover {
          width: 100%;
          height: 100%;
          position: absolute;
          top: 0;
          right: 0;
          display: flex;
          align-items: center;
          z-index: 999;
          justify-content: flex-start;
          left: 0;
          flex-direction: column;
          padding-top: 10px;
          .name {
            font-size: 20px;
            scale: 0.5;
            text-align: center;
          }
          .name2 {
            position: relative;
            top: -10px;
            width: 80px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          img {
            height: 38px;
            width: 52px;
          }
        }
      }
      .item2 {
        position: absolute;
        top: 0;
        left: 0;
      }
      .arrow-cover {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate3d(-50%, -50%, 0);
        width: 111px;
        height: 111px;
        z-index: 999;
      }
    }
  }
}
.award-list-wapprer {
  position: absolute;
  bottom: 24px;
  left: 40px;
  width: 296px;
  overflow-x: auto;
  display: flex;

  .award-list-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 102px;
    margin-right: 18px;
    &:last-of-type {
      margin-right: 0;
    }
    .award-list-item-img {
      width: 102px;
      height: 74px;
      img {
        width: 102px;
        height: 74px;
      }
    }
    .award-list-item-name {
      font-size: 14px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      height: 20px;
      width: 102px;
    }
  }
}
.left {
  position: absolute;
  left: 16px;
  width: 23px;
  bottom: 78px;
  height: 23px;
}
.right {
  position: absolute;
  right: 14px;
  width: 23px;
  bottom: 78px;
  height: 23px;
}
:deep(.empty .el-input__wrapper) {
  border: 1px solid red;
}
</style>
